<?php

class VcrTable extends Doctrine_Table
{
  public static function getInstance()
  {
    return Doctrine_Core::getTable('Vcr');
  }

  public function getBackendListQuery(Doctrine_Query $query)
  {
    $rootAlias = $query->getRootAlias();
    
    $user = sfContext::getInstance()->getUser();

    $query->innerJoin("$rootAlias.contestant c");
    
    if ($user->hasCredential('editor'))
    {
      $query->andWhere('publisher_id = ?', $user->getAttribute('id'));
    }
    else if ($user->hasCredential('admin'))
    {
      $query->andWhereIn('c.area_id', $user->getAreaIds());
    }
    
    return $query->innerJoin("$rootAlias.publisher");
  }
}
